package com.gai.shop.web.controller;

import com.gai.shop.common.Const;
import com.gai.shop.entity.Address;
import com.gai.shop.entity.User;
import com.gai.shop.service.AddressService;
import com.gai.shop.service.impl.AddressServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

public class AddressController extends BaseController {
    private final AddressService AS = new AddressServiceImpl();

    /**
     * 获取地址数据展示
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String showAddr(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute(Const.LOGIN_USER);
        if (user == null) {
            return Const.REDIRECT + "/login.jsp";
        }
        //uid
        List<Address> addressList = AS.selectAddressByUid(user.getUid());
        request.setAttribute(Const.UserConst.ADD_LIST,addressList);
        return Const.FORWARD + "/self_info.jsp";
    }

    /**
     * 新增地址
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String insertAddr(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute(Const.LOGIN_USER);
        if (user == null) {
            return Const.REDIRECT + "/login.jsp";
        }
        //获取前端传入的数据，封装到map里面中
        Map<String, String[]> parameterMap = request.getParameterMap();
        //声明
        Address address = new Address();
        try {
            //前端数据只有 收件人 收货地址 电话 uid astate
            BeanUtils.populate(address,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //设置新增地址的默认状态为非默认地址
        address.setAstate(0);
        //数据新增的业务
        AS.InsertIntoAddress(address);
        return Const.REDIRECT + "/addr?method=showAddr";
    }

    /**
     * 删除地址
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute(Const.LOGIN_USER);
        if (user == null) {
            return Const.REDIRECT + "/login.jsp";
        }
        //接收参数
        Integer aid = Integer.valueOf(request.getParameter("aid"));
        //直接使用aid到业务层做删除业务
        AS.deleteAddressByAid(aid);
        //回到获取数据的controller方法
        return Const.FORWARD + "/addr?method=showAddr";
    }

    /**
     * 修改地址
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String updateAddr(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute(Const.LOGIN_USER);
        if (user == null) {
            return Const.REDIRECT + "/login.jsp";
        }
        //获取参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        //声明address对象
        Address address = new Address();
        try {
            BeanUtils.populate(address,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //找到业务层进行数据修改
        AS.updateAddress(address);
        //回到获取数据的controller方法
        return Const.FORWARD + "/addr?method=showAddr";
    }

    /**
     * 设置地址的默认
     *  有2个动作
     *      1.把除了传递进来的aid以外的地址设置为普通 0
     *      2.把传递进来的aid的数据设置为默认 1
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String changeDefault(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute(Const.LOGIN_USER);
        if (user == null) {
            return Const.REDIRECT + "/login.jsp";
        }
        //获取参数
        Integer aid = Integer.valueOf(request.getParameter("aid"));
        //向业务层传递参数去处理修改默认地址
        AS.updateAddressByAidAndUidSetAstate(aid,user.getUid());
        //回到获取数据的controller方法
        return Const.FORWARD + "/addr?method=showAddr";
    }
}
